﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TakePhotoTogether.Utils;
using Microsoft.Devices;
using System.Diagnostics;
using TakePhotoTogether.Controller;
using System.Threading;


namespace TakePhotoTogether
{
    public partial class Welcome : PhoneApplicationPage
    {
        bool isSyncAnimation = false;
        bool isSplitAnimation = false;
       


        public Welcome()
        {
            InitializeComponent();

            MainController.Singleton.SetCurrentScreen("Welcome", GoMainPage);

            if (Utils.FtuFile.getState("state") != FtuFile.State.Closed)
            {
                MainPage.PhotoSecurityLock = false;
                GoMainPage();
            }
            else
            {
                MainPage.PhotoSecurityLock = true;
                this.LayoutRoot.Visibility = Visibility.Visible;
            }
        }

        private void CameraButtons_ShutterKeyReleased(object sender, EventArgs e)
        {
            Debug.WriteLine("Camera fisic button pressed at FTU");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MainPage.PhotoSecurityLock = false;
            GoMainPage();
        }

        private void GoMainPage()
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (NavigationService != null)
                {
                    NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
                }
            });
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            WelcomeFlipView.SelectedIndex = 5;
        }

        private void Button_Click_NEXT(object sender, RoutedEventArgs e)
        {
            WelcomeFlipView.SelectedIndex++;
        }

        private void HyperlinkButton_Click_1(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                Windows.System.Launcher.LaunchUriAsync(new Uri("settings:"));
            });
        }

        private void WelcomeFlipView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            FlipView item = (FlipView)sender;
            FlipViewItem selectedItem = (FlipViewItem)item.SelectedItem;

            if (selectedItem.Name == "Item3")
            {
                SyncAnimation.Begin();
                isSyncAnimation = true;
                //ButtonNextItem5.Visibility = Visibility.Visible;
                
                
            }
            else if (selectedItem.Name == "Item5")
            {
                SplitAnimation.Begin();
                isSplitAnimation = true;
                SplitAnimation.AutoReverse = false; 
                //ButtonNextItem5.Visibility = Visibility.Visible;
            }
            else
            {
                if (isSyncAnimation) 
                {
                    isSyncAnimation = false;
                    SyncAnimation.Stop();
                    ButtonNextItem3.Visibility = Visibility.Collapsed;
                }
                else if (isSplitAnimation) 
                {
                    isSplitAnimation = false;
                    SplitAnimation.Stop();
                    ButtonNextItem5.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void SyncAnimation_Completed(object sender, EventArgs e)
        {
            ButtonNextItem3.Visibility = System.Windows.Visibility.Visible;
            //SyncAnimation.RepeatBehavior = RepeatBehavior.Forever;
            SyncAnimation.Begin();
        }

        private void SplitAnimation_Completed(object sender, EventArgs e)
        {
            ButtonNextItem5.Visibility = System.Windows.Visibility.Visible;
            SplitAnimation.RepeatBehavior = RepeatBehavior.Forever;
            SplitAnimation.Begin();
        }

    }
}